<?php

namespace UploadFile\Listener;

use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use UploadFile\Service\UploadFile as UploadFileService;
use Zend\ServiceManager\ServiceLocatorInterface;
use UploadFile\Mapper\UploadFileInterface;
use UploadFile\Mapper\UploadFileLinkerInterface;

class SendListener implements ListenerAggregateInterface
{
    /**
     * @var ServiceLocatorInterface
     */
    protected $serviceLocator;
    protected $uploadFileMapper;
    protected $uploadFileLinkerMapper;
    
    public function __construct(ServiceLocatorInterface $serviceLocator) {
        $this->serviceLocator = $serviceLocator;
    }
    
    /**
     * @var \Zend\Stdlib\CallbackHandler[]
     */
    protected $listeners = array();

    /**
     * {@inheritDoc}
     */
    public function attach(EventManagerInterface $events)
    {
        $sharedEvents      = $events->getSharedManager();
        $this->listeners[] = $sharedEvents->attach('Admin\Service\AdminUser', 'createUser', array($this, 'onCreateUser'), 100);
    }

    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $index => $listener) {
            if ($events->detach($listener)) {
                unset($this->listeners[$index]);
            }
        }
    }

    public function onCreateUser($e)
    {
        $user = $e->getParams('user');
        $user = $user['user'];
        if($user) {
            /*--------------------------Upload_File------------------------------------------*/
            $userAvatar = $user->getAvatar();
            if($userAvatar)
            {
                $userAvatarPath = $userAvatar->getImagePath();
                if(!empty($userAvatarPath['tmp_name']))
                {
                    list($a , $b , $c , $imagePath) = explode('/' , $userAvatarPath['tmp_name']);
                    $userAvatar->setImagePath($imagePath);

                    $this->getUploadFileMapper()->insert($userAvatar);
                    /*--------------------------------------------------------------------*/

                    /*--------------------------Upload_File_Linker------------------------------------------*/
                    $userAvatarLinker = new \UploadFile\Entity\ImageLinker();
                    $userAvatarLinker->setImageId($userAvatar->getImageId());
                    $userAvatarLinker->setDetailedId($userAvatar->getImageId());
                    $userAvatarLinker->setObjectId($user->getUserId());
                    $userAvatarLinker->setObjectType('user_avatar');
                    $userAvatarLinker->setStatus('A');
                    $userAvatarLinker->setPosition(0);
                    $userAvatarLinker->setType('M');
                    $this->getUploadFileLinkerMapper()->insert($userAvatarLinker);
                    /*--------------------------------------------------------------------*/
                }
            }
        }
        return $user;
    }
    
     public function setUploadFileMapper(UploadFileInterface $uploadFileMapper)
    {
        $this->uploadFileMapper = $uploadFileMapper;
        return $this;
    }

    public function getUploadFileMapper()
    {
        if (null === $this->uploadFileMapper) {
            $this->uploadFileMapper = $this->serviceLocator->get('upload_file_mapper');
        }
        return $this->uploadFileMapper;

    }

    public function setUploadFileLinkerMapper(UploadFileLinkerInterface $uploadFileMapper)
    {
        $this->uploadFileLinkerMapper = $uploadFileMapper;
        return $this;
    }

    public function getUploadFileLinkerMapper()
    {
        if (null === $this->uploadFileLinkerMapper) {
            $this->uploadFileLinkerMapper = $this->serviceLocator->get('upload_file_linker_mapper');
        }
        return $this->uploadFileLinkerMapper;

    }
}
